package cn.edu.zafu.bigdata.yuyini;

public class ArithmeticExceptionExample {

    // 定义divide方法
    public static double divide(int a, int b) {
        if (b == 0) {
            // 当b为0时抛出ArithmeticException
            throw new ArithmeticException("除数不能为0");
        }
        return (double) a / b;
    }

    public static void main(String[] args) {
        try {
            int numerator = 10; // 被除数
            int denominator = 0; // 除数，这里设置为0来触发异常
            double result = divide(numerator, denominator);
            System.out.println("除法结果是: " + result);
        }
        catch (ArithmeticException e) {
            // 捕获ArithmeticException并输出错误信息
            System.out.println("发生错误: " + e.getMessage());
        }
    }
}